<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>班级抽人</title>
    <style>
        #bigdiv {
            width: 700px;
            height: 1000px;
            margin: 0 auto;
        }

        #aaa {
            width: 700px;
            height: 50px;
            margin: 0 auto;
            text-align: center;

        }

        #tab {
            margin: 0px auto;
            width: 500px;
            height: 500px;

        }

        td {
            border: solid 2px black;
            width: 50px;
            height: 50px;
            text-align: center;
            font-weight: bold;
        }
    </style>
    <script>
        var times = 0;
        var st;
        function fn() {
            times++;
            for (let i = 1; i <= 42; i++) {
                var tdObj = document.getElementById('stu' + i);
                tdObj.style.backgroundColor = 'white';
            }
            //获取随机数
            var num = parseInt(Math.random() * 41 + 1);
            console.log(times + ':' + num);
            //获取对应的td对象
            var tdObj = document.getElementById('stu' + num);
            //设置背景颜色
            tdObj.style.backgroundColor = 'red';

            if (times >= 30) {
                // tdObj.style.backgroundColor = 'white';
                // tdObj = document.getElementById('stu' +29 );
                // tdObj.style.backgroundColor = 'red';
                if (num == 19) {
                    console.log('aaa');
                    times = times - 1;
                } else {
                    document.getElementById('bbb').innerText = '抽中' + tdObj.innerText;
                    clearInterval(st);
                }
            }
        }
        function start() {
            times = 0;
            for (let i = 1; i <= 42; i++) {
                var tdObj = document.getElementById('stu' + i);
                tdObj.style.backgroundColor = 'white';
            }
            document.getElementById('bbb').innerText = '正在抽人中........'
            st = setInterval(fn, 100);
        }
    </script>
</head>

<body>
    <!-- setInterval: 班级回答问题 -->
    <div id="aaa">
        <button onclick="start()">开始选人</button><span id="bbb"></span>

    </div>
    <div id="bigdiv">

        <table id="tad">
            <tr>
                <td id="stu1">李韦峰</td>
                <td id="stu2">卢国建</td>
                <td id="stu3">郑炜喆</td>
                <td id="stu4">占志新</td>
                <td id="stu5">陈梅香</td>
                <td id="stu6">廖治先</td>
                <td id="stu7">廖柏成</td>
                <td id="stu8">李涛</td>
                <td id="stu9">陈昊童</td>
                <td id="stu10">陈华伟</td>
                <td id="stu11">郑文源</td>
            </tr>
            <tr>
                <td id="stu12">郑宗帅</td>
                <td id="stu13">陈立智</td>
                <td id="stu14">刘永潘</td>
                <td id="stu15">吴文龙</td>
                <td id="stu16">陈鹏</td>
                <td id="stu17">白婉婷</td>
                <td id="stu18">王世财</td>
                <td id="stu19">庄云</td>
                <td id="stu20">杨凌翔</td>
                <td id="stu21">林世涛</td>
                <td id="stu22">张淑芳</td>
            </tr>
            <tr>
                <td id="stu23">戴俊锋</td>
                <td id="stu24">谢金金</td>
                <td id="stu25">赵浩敏</td>
                <td id="stu26">袁贵森</td>
                <td id="stu27">黄雄</td>
                <td id="stu28">吴欣燕</td>
                <td id="stu29">陆利群</td>
                <td id="stu30">罗此东</td>
                <td id="stu31">韦仲晓</td>
                <td id="stu32">黄柱菘</td>
                <td id="stu33">黄富贵</td>
            </tr>
            <tr>
                <td id="stu34">张正豪</td>
                <td id="stu35">张先杰</td>
                <td id="stu36">陆建锋</td>
                <td id="stu37">张耀仁</td>
                <td id="stu38">胡基耀</td>
                <td id="stu39">唐皓颖</td>
                <td id="stu40">罗启恒</td>
                <td id="stu41">马鑫涛</td>
                <td id="stu42">曾德森</td>
            </tr>
        </table>
    </div>
</body>

</html>